import plugin from '../../lib/plugins/plugin.js'
import { segment } from "oicq";
import fetch from 'node-fetch'
export class example extends plugin {
  constructor() {
    super({
      name: '生日',
      dsc: '今天谁生日',
      event: 'message',
      priority: 5000,
      rule: [
        {
          reg: '^(今|明|后)天谁生日$',
          fnc: 'birthday'
        }
      ]

    })
  }


  async birthday(e) {
    let ck = getck("qun.qq.com")
    let to = 0;
    if (/明/.test(e.msg)) {
      to = 1
    } else if (/后/.test(e.msg)) {
      to = 2
    }
    let url = `http://xiaobai.klizi.cn/API/qqgn/qun_birthday.php?uin=${Bot.uin}&skey=${ck.skey}&pskey=${ck.p_skey}&group=${e.group_id}`
    let res = await fetch(url).then(res => res.json()).catch(err => console.log(err))
    if (!res) return e.reply("接口错误")
    let time = {
      0: "今",
      1: "明",
      2: "后"
    }
    res = res.data.event_info
    let today = Object.keys(res)[to]

    if (!today) return e.reply(`${time[to]}天没有人生日哦`)

    let todays = res[today][0]
    let birthday = todays.birthday


    let msg = [
      `${todays.name.replace(/今|明|后/, time[to])}\n`,
      `${todays.desc}`,
    ]
    await e.reply(msg)
    for (let i of birthday) {
      await e.reply([
        segment.image(`https://q1.qlogo.cn/g?b=qq&s=100&nk=${i.uin}`),
        `QQ：${i.uin}\n`,
        `昵称：${i.display_name}`,

      ])
    }
  }

}
function getck(data) {
  let cookie = Bot.cookies[data]
  let ck = cookie.replace(/=/g, `":"`).replace(/;/g, `","`).replace(/ /g, "").trim()
  ck = ck.substring(0, ck.length - 2)
  ck = `{"`.concat(ck).concat("}")
  return JSON.parse(ck)
}